package cn.itcast.controller.system;

import cn.itcast.controller.BaseContorller;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseContorller {

    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;


    @RequestMapping(value = "/list",name = "分页展示员工数据显示")
    public String findAll(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize){
        PageInfo pageInfo =userService.findPage(getCompanyId(),pageNum,pageSize);
        request.setAttribute("page",pageInfo);
        return "system/user/user-list";
    }

    @RequestMapping(value = "/toAdd",name = "跳转添加员工")
    public String toAdd(){
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);
        return "system/user/user-add";
    }


    @RequestMapping(value = "/edit",name = "保存员工")
    public String edit(User user){

        if (StringUtils.isEmpty(user.getId())){//id为空
            user.setId(UUID.randomUUID().toString());
            user.setCompanyId(getCompanyId());
            user.setCompanyName(getCompanyName());
            userService.save(user);

        }else {//id不为空
            userService.update(user);
        }


        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "/toUpdate",name = "进入员工修改页面")
    public String update(String id){
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);
        User user = userService.findById(id);
        request.setAttribute("user",user);
        return "system/user/user-add";
    }

    @RequestMapping(value = "/delete",name = "进入员工修改页面")
    public String delete(String id){

        userService.deleteById(id);

        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "/roleList",name = "进入用户权限页面")
    public String roleList(String id){
        User user = userService.findById(id);
        request.setAttribute("user",user);
        List<Role> roleList = roleService.findAll(getCompanyId());
        request.setAttribute("roleList",roleList);
        List<String> list =  userService.findByUserId(id);
        /*String userRoleStr ="";
        for (String s : list) {
            userRoleStr +=s+",";
        }*/
        String userRoleStr = list.stream().collect(Collectors.joining(","));
        request.setAttribute("userRoleStr",userRoleStr);
        return "system/user/user-role";
    }
    @RequestMapping(value = "/changeRole",name = "保存用户权限")
    public String changeRole(String userid,String[] roleIds){
        userService.changeRole(userid,roleIds);
        return "redirect:/system/user/list.do";
    }


}
